package com.ja10.books;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;

public class ReserveList {
	
	private int id;
	private int userID;
	private String genreName;
	private String publisherName;
	private String title;
	private String writer;
	private String content;
	private String publicationDate;
	private Bitmap pictureBmp;
	private byte[] pictureByte;
	private int rental_id;
	private String isbn;
	@SuppressWarnings("unused")
	private String genre;
	@SuppressWarnings("unused")
	private String publisher;
	private RentalList rentalBean;
	private Date reserveDate;
	private String reserveDate_str;
	private String reservePlanDate;
	private BookList bookBean;
	private BookList reserveBean;
//	private member.MemberBean memberBean;
//	private ReserveBean reserveBean;
	
// 画像フォルダのパスを設定
	final static String PATH = "http://192.168.3.190:8080/Books/book_img/";
	
	ReserveDay reserve_day 	= new ReserveDay();
	Calendar calendar 		= Calendar.getInstance(); 
		
		
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	
	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	public String getGenreName() {
		return genreName;
	}
	public void setGenreName(String genreName) {
		this.genreName = genreName;
	}
	public String getPublisherName() {
		return publisherName;
	}
	public void setPublisherName(String publisherName) {
		this.publisherName = publisherName;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getPublicationDate() {
		return publicationDate;
	}
	public void setPublicationDate(String publicationDate) {
		this.publicationDate = publicationDate;
	}
	public Bitmap getPictureBmp() {
		return pictureBmp;
	}
	public void setPictureBmp(String picture, int size) throws IOException {
		// 画像のパスからビットマップの生成
		URL url = new URL(PATH + picture + ".jpg");
		InputStream input= url.openStream();
		Bitmap bm = BitmapFactory.decodeStream(input);
		// 画像のサイズ変更
		int w = bm.getWidth();
		int h = bm.getHeight();
		float scale = Math.min((float)size/w, (float)size/h);
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		
		this.pictureBmp = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
	}
	public void setPictureBmp(byte[] picture) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		this.pictureBmp = BitmapFactory.decodeByteArray(picture, 0, picture.length, options);
	}
	public byte[] getPictureByte() {
		return pictureByte;
	}
	public void setPictureByte(Bitmap picture) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		picture.compress(CompressFormat.JPEG, 100, os);

		this.pictureByte = os.toByteArray();
	}
	public int getRental_id() {
		return rental_id;
	}
	public void setRental_id(int rental_id) {
		this.rental_id = rental_id;
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	public String getGenre() {
		return genre;
	}
	public void setGenre(String genre) {
		this.genre = genre;
	}
	public String getPublisher() {
		return publisher;
	}
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	public RentalList getRentalBean() {
		return rentalBean;
	}
	public void setRentalBean(RentalList rentalBean) {
		this.rentalBean = rentalBean;
	}
	public Date getReserveDate() {
		return reserveDate;
	}
	public void setReserveDate(String reserveDate) {
		
		calendar = reserve_day.day(reserveDate);
		
		this.reserveDate = calendar.getTime();
	}
	
	
	
	public String getReserveDate_str() {
		return reserveDate_str;
	}
	public void setReserveDate_str(String reserveDate_str) {
		this.reserveDate_str = reserveDate_str;
	}
	public String getReservePlanDate() {
		return reservePlanDate;
	}
	public void setReservePlanDate(String reservePlanDate) {
		this.reservePlanDate = reservePlanDate;
	}
	public void setPictureBmp(Bitmap pictureBmp) {
		this.pictureBmp = pictureBmp;
	}
	public void setPictureByte(byte[] pictureByte) {
		this.pictureByte = pictureByte;
	}
	public BookList getBookBean() {
		return bookBean;
	}
	public void setBookBean(BookList bookBean) {
		this.bookBean = bookBean;
	}
	public BookList getReserveBean() {
		return reserveBean;
	}
	public void setReserveBean(BookList reserveBean) {
		this.reserveBean = reserveBean;
	}
	
	public String getData(String sql) throws java.sql.SQLException {

		Connection con = null;
		Statement smt = null;

		try {
			con = DBManager.getConnection();
			smt = con.createStatement();
			ResultSet rs = smt.executeQuery(sql);
			rs.first();

			return rs.getString("NAME");

		} catch(SQLException e) {
			throw new java.sql.SQLException(e);
		} finally {
			if(smt != null) {
				try {
					smt.close();
				} catch(SQLException ignore) {
					// none
				}
			}
			if(con != null) {
				try {
					con.close();
				} catch(SQLException ignore) {
					// none
				}
			}
		}
	}

}